package com.rljk.aspect;

import com.rljk.common.result.CodeMsg;
import com.rljk.exception.GlobalException;
import com.rljk.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Author:   YASUO
 * Date:     2018/6/27 13:58
 * Description:
 */


@Slf4j
//@Aspect
@Component
public class UserAuthorizeAspect {

//    @Autowired
//    private RedisService redisService;注入不了service

    @Autowired
    private RedisService redisService;

    @Pointcut("execution(public * com.rljk.controller.app.*.*(..))" +
            "&& !execution(public * com.rljk.controller.app.UserInController.*(..))"+
            "&& !execution(public * com.rljk.controller.app.OtoInController.*(..))"+
            "&& !execution(public * com.rljk.controller.app.AssetsInController.*(..))"+
            "&& !execution(public * com.rljk.controller.app.HuoBiInController.*(..))"+
            "&& !execution(public * com.rljk.controller.app.ThirdLoginInController.*(..))"+
            "&& !execution(public * com.rljk.controller.app.CouponInController.*(..))"+
            "&& !execution(public * com.rljk.controller.app.CommentInController.commentList(..))"+
            "&& !execution(public * com.rljk.controller.app.HomeInController.*(..))")

    public void verify() {}

    @Before("verify()")
    @ResponseBody
    public void doVerify() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        log.info("@Aspect开始校验");

        //去redis里查询
        String TOKEN = request.getParameter("TOKEN");
//        String s = redisService.get(TOKEN);
//        log.info("==========" + s);
        if (StringUtils.isEmpty(TOKEN)) {
            log.warn("【登录校验】Redis中查不到token");
            throw new GlobalException(CodeMsg.TOKEN_ERROR);
            //return Result.error(CodeMsg.TOKEN_ERROR);
        }
    }
}
